4 * @subpackage SpecialPage
7 class contribs_finder
{
8 var $username, $offset, $limit, $namespace, $invert;
11 function contribs_finder($username) {
12 $this->username
= $username;
13 $this->dbr
=& wfGetDB(DB_SLAVE
);
16 function set_limit($limit) {
17 $this->limit
= $limit;
20 function set_offset($offset) {
21 $this->offset
= $offset;
24 function set_namespace($namespace, $invert = false) {
25 $this->namespace = $namespace;
26 $this->invert
= $invert;
29 function set_hide_minor($hm) {
30 $this->hide_minor
= $hm;
33 function get_edit_limits() {
34 list($index, $usercond) = $this->get_user_cond();
36 $use_index = $this->dbr
->useIndexClause($index);
37 #$sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
38 # "FROM page, revision $use_index WHERE page_id = rev_page " .
42 #$sql .= $this->get_namespace_cond();
43 #$sql .= $this->get_minor_cond();
44 $sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
45 "FROM revision $use_index WHERE " . $usercond;
47 $res = $this->dbr
->query($sql, "contribs_finder::get_edit_limits");
49 while ($o = $this->dbr
->fetchObject($res))
51 $row = $rows[count($rows) - 1];
52 return array($row->earliest
, $row->latest
);
55 function get_user_cond() {
58 if ($this->username
== 'newbies') {
59 $max = $this->dbr
->selectField('user', 'max(user_id)', false, "make_sql");
60 $condition = '>' . ($max - $max / 100);
63 if ($condition == "") {
64 $condition = " rev_user_text=" . $this->dbr
->addQuotes($this->username
);
65 $index = 'usertext_timestamp';
67 $condition = " rev_user {$condition}";
68 $index = 'user_timestamp';
71 return array($index, $condition);
74 function get_minor_cond() {
75 if ($this->hide_minor
)
76 return ' AND rev_minor_edit=0';
80 function get_namespace_cond() {
81 $nsQuery = $nsinvert = "";
86 if (!is_null($this->namespace))
87 $nsQuery .= "AND page_namespace {$nsinvert}= {$this->namespace}";
92 function get_previous_offset_for_paging() {
93 list($index, $usercond) = $this->get_user_cond();
94 $use_index = $this->dbr
->useIndexClause($index);
96 $sql = "SELECT rev_timestamp FROM page, revision $use_index " .
97 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset
. "' AND " .
98 "rev_user_text = " . $this->dbr
->addQuotes($this->username
);
99 $sql .= $this->get_namespace_cond();
100 $sql .= $this->get_minor_cond();
101 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . $this->limit
;
102 $res = $this->dbr
->query($sql);
104 while ($obj = $this->dbr
->fetchObject($res))
106 $this->dbr
->freeResult($res);
107 return $rows[count($rows) - 1]->rev_timestamp
;
110 function get_first_offset_for_paging() {
111 list($index, $usercond) = $this->get_user_cond();
112 $use_index = $this->dbr
->useIndexClause($index);
114 $sql = "SELECT rev_timestamp FROM page, revision $use_index " .
115 "WHERE page_id = rev_page AND " .
116 "rev_user_text = " . $this->dbr
->addQuotes($this->username
);
117 $sql .= $this->get_namespace_cond();
118 $sql .= $this->get_minor_cond();
119 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . ($this->limit +
1);
120 $res = $this->dbr
->query($sql);
122 while ($obj = $this->dbr
->fetchObject($res))
124 $this->dbr
->freeResult($res);
125 return $rows[count($rows) - 1]->rev_timestamp
;
128 /* private */ function make_sql() {
129 $userCond = $condition = $index = $minorQuery = $nsQuery
130 = $offsetQuery = $limitQuery = $nsinvert = "";
132 $minorQuery = $this->get_minor_cond();
133 $nsQuery = $this->get_namespace_cond();
135 extract($this->dbr
->tableNames('page', 'revision'));
136 list($index, $userCond) = $this->get_user_cond();
138 $limitQuery = "LIMIT {$this->limit}";
140 $offsetQuery = "AND rev_timestamp < '{$this->offset}'";
142 $use_index = $this->dbr
->useIndexClause($index);
144 page_namespace,page_title,page_is_new,page_latest,
145 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
147 FROM $page,$revision $use_index
148 WHERE page_id=rev_page AND $userCond $minorQuery $nsQuery $offsetQuery
149 ORDER BY rev_timestamp DESC $limitQuery";
155 $res = $this->dbr
->query($this->make_sql(), "contribs_finder::find");
156 while ($c = $this->dbr
->fetchObject($res))
158 $this->dbr
->freeResult($res);
164 * Special page "user contributions".
165 * Shows a list of the contributions of a user.
168 * @param string $par (optional) user name of the user for which to show the contributions
170 function wfSpecialContributions( $par = null ) {
171 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
172 $fname = 'wfSpecialContributions';
174 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
175 if (!strlen($target)) {
176 $wgOut->errorpage('notargettitle', 'notargettext');
180 $nt = Title
::newFromURL( $target );
182 $wgOut->errorpage( 'notargettitle', 'notargettext' );
185 $nt =& Title
::makeTitle(NS_USER
, $nt->getDBkey());
187 $namespace = $wgRequest->getIntOrNull('namespace');
188 $invert = $wgRequest->getBool('invert');
189 $hideminor = $wgRequest->getBool('hideminor');
190 $limit = min($wgRequest->getInt('limit', 50), 500);
191 $offset = $wgRequest->getVal('offset');
192 /* Offset must be an integral. */
193 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
196 $title = Title
::makeTitle(NS_SPECIAL
, "Contributions");
197 $urlbits = "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode($target);
198 $myurl = $title->escapeLocalURL($urlbits);
200 $finder = new contribs_finder($nt->getText());
202 $finder->set_namespace($namespace, $invert);
203 $finder->set_hide_minor($hideminor);
204 $finder->set_limit($limit);
205 $finder->set_offset($offset);
207 if ($wgRequest->getText('go') == "prev") {
208 $prevts = $finder->get_previous_offset_for_paging();
209 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
210 $wgOut->redirect($prevurl);
214 if ($wgRequest->getText('go') == "first") {
215 $prevts = $finder->get_first_offset_for_paging();
216 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
217 $wgOut->redirect($prevurl);
221 $sk = $wgUser->getSkin();
223 $id = User
::idFromName($nt->getText());
226 $ul = $nt->getText();
228 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
229 $userCond = '=' . $id;
231 $talk = $nt->getTalkPage();
233 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK
) ) . ')';
236 if ($target == 'newbies') {
237 $ul = wfMsg ('newbies');
240 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
242 $contribsPage = Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
243 $mlink = $sk->makeKnownLinkObj( $contribsPage,
244 $hideminor ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
246 'target' => $nt->getPrefixedDbKey(),
249 'hideminor' => $hideminor ?
0 : 1,
250 'namespace' => $namespace ) ) );
252 $contribs = $finder->find();
254 if (count($contribs) == 0) {
255 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
259 list($early, $late) = $finder->get_edit_limits();
260 $lastts = count($contribs) ?
$contribs[count($contribs) - 1]->rev_timestamp
: 0;
261 $atstart = (!count($contribs) ||
$late == $contribs[0]->rev_timestamp
);
262 $atend = (!count($contribs) ||
$early == $lastts);
263 wfdebug("early=$early late=$late lastts=$lastts\n");
264 $wgOut->addHTML(ucNamespaceForm($target, $hideminor, $namespace, $invert));
266 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
268 $firsttext = wfMsgHtml("histfirst");
269 $lasttext = wfMsgHtml("histlast");
271 $prevtext = wfMsg("prevn", $limit);
273 $lastlink = $lasttext;
274 $prevlink = $prevtext;
276 $lastlink = "<a href=\"$myurl&limit=$limit\">$lasttext</a>";
277 $prevlink = "<a href=\"$myurl&offset=$offset&limit=$limit&go=prev\">$prevtext</a>";
280 $nexttext = wfMsg("nextn", $limit);
282 $firstlink = $firsttext;
283 $nextlink = $nexttext;
285 $firstlink = "<a href=\"$myurl&limit=$limit&go=first\">$firsttext</a>";
286 $nextlink = "<a href=\"$myurl&offset=$lastts&limit=$limit\">$nexttext</a>";
288 $firstlast = "($lastlink | $firstlink)";
291 foreach (array(20, 50, 100, 250, 500) as $num)
292 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
293 $bits = implode($urls, ' | ');
295 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
297 $shm = wfMsgHtml( "contribs-showhideminor", $mlink );
298 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)</p>\n");
300 $wgOut->addHTML( "<ul>\n" );
302 foreach ($contribs as $contrib)
303 $wgOut->addHTML(ucListEdit($sk, $contrib));
305 $wgOut->addHTML( "</ul>\n" );
306 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)\n");
311 * Generates each row in the contributions list.
313 * Contributions which are marked "top" are currently on top of the history.
314 * For these contributions, a [rollback] link is shown for users with sysop
315 * privileges. The rollback link restores the most recent version that was not
316 * written by the target user.
318 * If the contributions page is called with the parameter &bot=1, all rollback
319 * links also get that parameter. It causes the edit itself and the rollback
320 * to be marked as "bot" edits. Bot edits are hidden by default from recent
321 * changes, so this allows sysops to combat a busy vandal without bothering
324 * @todo This would probably look a lot nicer in a table.
326 function ucListEdit( $sk, $row ) {
327 $fname = 'ucListEdit';
328 wfProfileIn( $fname );
330 global $wgLang, $wgOut, $wgUser, $wgRequest;
332 if( !isset( $messages ) ) {
333 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
334 $messages[$msg] = wfMsg( $msg );
338 $page =& Title
::makeTitle( $row->page_namespace
, $row->page_title
);
339 $link = $sk->makeKnownLinkObj( $page, '' );
340 $difftext = $topmarktext = '';
341 if( $row->rev_id
== $row->page_latest
) {
342 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
343 if( !$row->page_is_new
) {
344 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
346 $difftext .= $messages['newarticle'];
349 if( $wgUser->isAllowed('rollback') ) {
350 $extraRollback = $wgRequest->getBool( 'bot' ) ?
'&bot=1' : '';
351 $extraRollback .= '&token=' . urlencode(
352 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text
) ) );
353 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
354 $messages['rollbacklink'],
355 'action=rollback&from=' . urlencode( $row->rev_user_text
) . $extraRollback ) .']';
359 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
360 $difftext = '(' . $messages['diff'] . ')';
362 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id
);
364 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
366 $comment = $sk->commentBlock( $row->rev_comment
, $page );
367 $d = $wgLang->timeanddate( $row->rev_timestamp
, true );
369 if( $row->rev_minor_edit
) {
370 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
375 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
376 if( $row->rev_deleted
) {
377 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
379 $ret = "<li>$ret</li>\n";
380 wfProfileOut( $fname );
385 * Generates a form used to restrict display of contributions
386 * to a specific namespace
389 * @param string $target target user to show contributions for
390 * @param string $hideminor whether minor contributions are hidden
391 * @param string $namespace currently selected namespace, NULL for show all
392 * @param bool $invert inverts the namespace selection on true (default null)
394 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
395 global $wgContLang, $wgScript;
397 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
398 $namespaceselect .= '<option value="" '.(is_null($namespace) ?
' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
399 $arr = $wgContLang->getFormattedNamespaces();
400 foreach( $arr as $ns => $name ) {
403 $n = $ns === NS_MAIN ?
wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
404 $sel = $namespace == $ns ?
' selected="selected"' : '';
405 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
407 $namespaceselect .= '</select>';
409 $action = htmlspecialchars( $wgScript );
410 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
411 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
412 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
413 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ?
1 : 0 ) .'" />';
415 <div id='nsselect' class='contributions'>
416 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
418 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
419 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . " />
420 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
422 $out .= '</form></div>';